//
//  CollectionViewController.m
//  lisure0.1
//
//  Created by xalo on 16/4/22.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "CollectionViewController.h"
#import "ReadListTableViewCell.h"
#import "CollectionModel.h"


@interface CollectionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)UITableView* tableView;
@property(nonatomic, retain)NSMutableArray* array;


@end

@implementation CollectionViewController

-(UITableView *)tableView{

    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(NSMutableArray *)array{

    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    [self.array addObjectsFromArray:[[DataManager defauleDataManager] selectedData]];
//    NSLog(@"%@",self.array);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
    [self.tableView registerNib:[UINib nibWithNibName:@"ReadListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ReadListTableViewCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.tableView showMessage:@"没有收藏" byDataSourceCount:self.array.count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadListTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ReadListTableViewCell" forIndexPath:indexPath];
    CollectionModel* model = [self.array objectAtIndex:indexPath.row];
    [cell.coverimg sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
    cell.title.text = model.title;
    cell.content.text = model.content;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[DataManager defauleDataManager].context deleteObject:self.array[indexPath.row]];
        [[DataManager defauleDataManager].context save:nil];
        [self.array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];        
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
